%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 13. 4月 2023 19:49
%%%-------------------------------------------------------------------
-module(sc_event_logger).
-behaviour(gen_event).
-export([add_handler/0, delete_handler/0]).
-export([init/1, handle_event/2, handle_call/2,
  handle_info/2, code_change/3, terminate/2]).
-record(state, {}).
init([]) ->
  {ok, #state{}}.

add_handler() -> % ❶ (以下3行)add_handler/0和delete_handler/0函数
  sc_event:add_handler(?MODULE, []).
delete_handler() ->
  sc_event:delete_handler(?MODULE, []).
handle_event({create, {Key, Value}}, State) ->
  error_logger:info_msg("create(~w, ~w)~n", [Key, Value]), % ❷ error_logger消息
  {ok, State};
handle_event({lookup, Key}, State) ->
  error_logger:info_msg("lookup(~w)~n", [Key]),
  {ok, State};
handle_event({delete, Key}, State) ->
  error_logger:info_msg("delete(~w)~n", [Key]),
  {ok, State};
handle_event({replace, {Key, Value}}, State) ->
  error_logger:info_msg("replace(~w, ~w)~n", [Key, Value]),
  {ok, State}.

handle_call(_Request, State) ->
  Reply = ok,
  {ok, Reply, State}.
handle_info(_Info, State) ->
  {ok, State}.
terminate(_Reason, _State) ->
  ok.
code_change(_OldVsn, State, _Extra) ->
  {ok, State}.